(function($){
	var KEY = "jforest.iframe";

	var Iframe = function(element,options){
		this.$element = $(element);
		this.options = $.extend({},{
			baseUrl:'http://www.lib76.com',
			url:'',
			width:'100%',
			height:'auto',
			minHeight:300,
			loadCallback:function(){}
		},options);
		var size = 'width="' + this.options.width+'"';
		if(this.options.height != 'auto'){
			size = size + 'height="'+height+'"';
		}
		this.$iframe = $('<iframe src="'+this.options.url+'" frameborder="0" '+size+' marginheight="0" marginwidth="0" scrolling="no"></iframe>');
		var self = this;
		this.$iframe.on('load',function(){
			self._loadCallback.call(self,this);
		}).appendTo(this.$element);
		//获取当前的路径，截取域名
	};

	Iframe.prototype = {
		_loadCallback:function(iframe){
			var self = this,options = this.options;
			if (iframe.contentDocument){
				if(iframe.contentDocument.body.offsetHeight){
					iframe.height = iframe.contentDocument.body.offsetHeight;
				}
		    }else if(iframe.Document){
		    	if(iframe.Document.body.scrollHeight){
		    		iframe.height = iframe.Document.body.scrollHeight;
		    	}
		    }
		    if(!iframe.height || iframe.height<options.minHeight){
		    	iframe.height = options.minHeight;
		    }
		    //触发回调
		    if(options.loadCallback){
		    	options.loadCallback.call(iframe);
		    }
		},
		load:function(url){
			var self = this,options = this.options;
			var iframe = this.$iframe[0];
			if(iframe.contentWindow){
				if(url){
					if(url.indexOf('http://')>0){
						iframe.contentWindow.location.href = url;
					}else if(iframe.contentWindow.location.protocol && iframe.contentWindow.location.protocol != 'about:'){
						iframe.contentWindow.location.href = iframe.contentWindow.location.protocol + '//' + iframe.contentWindow.location.host + url;
					}else{
						iframe.contentWindow.location.href = options.baseUrl + url;
					}
				}
			}else if(iframe.window){
				if(url){
					if(url.indexOf('http://')>0){
						iframe.window.location.href = url;
					}else if(iframe.window.location.protocol && iframe.window.location.protocol != 'about:'){
						iframe.window.location.href = iframe.window.location.protocol + '//' + iframe.window.location.host + url;
					}else{
						iframe.window.location.href = options.baseUrl + url;
					}
				}
			}
		}
	};
	
	$.fn.jfIframe = function(){
		var args = Array.prototype.slice.call(arguments, 0);
		var value = undefined;
		this.each(function () {
			var $this = $(this);
			if (args.length === 0 || typeof(args[0]) === "object") {
				opts = args.length === 0 ? {} : $.extend({}, args[0]);
				$this.data(KEY,new Iframe(this,opts)); //创建控件
			}else if (typeof(args[0]) === "string"){
				var entity = $this.data(KEY);
                if(entity === undefined){return;}
                value = entity[args[0]].apply(entity, args.slice(1));
			}else{
				 throw 'Invalid arguments to '+KEY+' plugin: ' + args;
			}
	    });
	    return (value === undefined) ? this : value;
	};
	
	$.fn.jfIframe.Constructor = Iframe;
	
})(window.jQuery)